home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_c / pclc41.zip / SIMPLE.C < prev    next >
Text File  |  1994-04-18  |  2KB  |  86 lines

  1. /*
  2. **                ---  simple.c ---
  3. **
  4. **  EXAMPLE CODE: This example is meant to be the simpliest
  5. **  possible terminal emulator. Whatever is typed is sent out
  6. **  over the selected serial port, and whatever is received from
  7. **  the serial port is displayed on the screen.
  8. **
  9. **  This example program (not the PCL4C library) is donated to
  10. **  the Public Domain by MarshallSoft Computing, Inc. It is
  11. **  provided as an example of the use of the PCL4C.
  12. **
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include "pcl4c.h"
  18.  
  19. #define FALSE 0
  20. #define TRUE !FALSE
  21. #define ESC 0x1b
  22.  
  23. /*** Global Variables ***/
  24.  
  25. int Port = 0;             /* COM port # 0 ( COM1 ) */
  26. int BaudCode = Baud2400;  /* Code for 2400 baud  */
  27. char RxBuf[128];          /* PCL receive buffer  */
  28. char TxBuf[128];          /* PCL transmit buffer  */
  29.  
  30. int ErrorCheck(int Code);
  31.  
  32. /*** Main ***/
  33.  
  34. void main(int argc, char *argv[])
  35. {
  36.  char c;
  37.  int i, rc;
  38.  if(argc!=2)
  39.    {printf("Usage: SIMPLE port\n");
  40.     exit(1);
  41.    }
  42.  /* get port number from command line */
  43.  Port = atoi(argv[1]) - 1;
  44.  if((Port<0) || (Port>3))
  45.      {printf("Port must be COM1 to COM4\n");
  46.       exit(1);
  47.      }
  48.  /* setup transmit & receive buffer */
  49.  ErrorCheck( SioRxBuf(Port,RxBuf,Size128) );
  50.  /* set port parmameters */
  51.  ErrorCheck( SioParms(Port,NoParity,OneStopBit,WordLength8) );
  52.  /* reset the port */
  53.  ErrorCheck( SioReset(Port,BaudCode) );
  54.  printf("\nCOM%d @ 2400 Baud\n",1+Port);
  55.  /* set DTR and RTS */
  56.  ErrorCheck( SioDTR(Port,'S') );
  57.  ErrorCheck( SioRTS(Port,'S') );
  58.  
  59.  printf("Enter terminal loop ( Type ESC to exit )\n");
  60.  /* enter terminal loop */
  61.  while(TRUE)
  62.      {/* was key pressed ? */
  63.       if(SioKeyPress())
  64.           {i = SioKeyRead();
  65.            if((char)i==ESC)
  66.               {/* restore COM port status & exit */
  67.                SioDone(Port);
  68.                exit(1);
  69.               }
  70.            else SioPutc(Port,(char)i);
  71.           } /* end if */
  72.       /* any incoming over serial port ? */
  73.       i = SioGetc(Port,0);
  74.       if(i>-1) SioCrtWrite((char)i);
  75.      } /* end while */
  76. } /* end main */
  77.  
  78. int ErrorCheck(int Code)
  79. {/* trap PCL error codes */
  80.  if(Code<0)
  81.      {SioError(Code);
  82.       SioDone(Port);
  83.       exit(1);
  84.      }
  85.  return(0);
  86. } /* end ErrorCheck */